<?php
class ViAccomodationType_Controller extends AdminController {

    public function __construct() {
        parent::__construct('viAccomodationType');
        $this->setDefaultListOrder('code');
    }

    function all() {
        $this->initPagination('vi_accomodation_type');

        $this->viewData['viAccomodationTypeList'] = $this->dao->queryAll(
                $this->orderLimitAndOffset('SELECT * FROM vi_accomodation_type'));

        $this->renderSubview('admin/viAccomodationType/viAccomodationTypeList');
    }

    function display() {
        $code = $this->input->get('code');

        $viAccomodationType = $this->dao->getByCode('vi_accomodation_type', $code);
        if (! $viAccomodationType) {
            $this->redirectToList('error.recordNotFound');
        }
        $this->viewData['viAccomodationType'] = $viAccomodationType;
        $this->renderSubview('admin/viAccomodationType/viAccomodationTypeDisplay');
    }

    function edit() {
        $code = $this->input->get('code');
        $viAccomodationType = array();

        if ($code) {
            $viAccomodationType = $this->dao->getByCode('vi_accomodation_type', $code);
            if (! $viAccomodationType) {
                $this->msg->error('error.recordNotFound');
                $this->redirect('/admin/viAccomodationType/all');
            }
        }
        else {
            // Set default values for a new record here
        }

        $this->loadFormData();

        $this->viewData['viAccomodationType'] = $viAccomodationType;
        $this->renderSubview('admin/viAccomodationType/viAccomodationTypeEdit');
    }

    protected function loadFormData() {
    }

    function save() {
        $viAccomodationType = $this->postToDbValues();
        $code = $viAccomodationType['code'];
        $this->viewData['viAccomodationType'] = $viAccomodationType;

        // Basic validation
        $validator = new EntityValidator($viAccomodationType, 'viAccomodationType', $this->msg);
        $validator->required('code');
        $validator->required('name');
        
        if ($this->msg->hasErrors()) {
            $this->loadFormData();
            $this->publishErrors('admin/viAccomodationType/viAccomodationTypeEdit');
            return;
        }

        // Database validation - unique name
        if ($this->dao->alreadyExists('vi_accomodation_type', 'name', $viAccomodationType, 'code')) {
            $this->msg->error('viAccomodationType.error-alreadyExists-name', $viAccomodationType['name']);
        }
        // Database validation - unique code
        if (! sql::isPersistent($viAccomodationType)) {
            $existingEntity = $this->dao->getByCode('vi_accomodation_type', $code);
            if ($existingEntity) {
                $this->msg->error('viAccomodationType.error-alreadyExists-code', $code);
            }
        }
        if ($this->msg->hasErrors()) {
            $this->loadFormData();
            $this->publishErrors('admin/viAccomodationType/viAccomodationTypeEdit');
            return;
        }

        // Insert or update
        if (sql::isPersistent($viAccomodationType)) {
            $this->dao->update('vi_accomodation_type', $viAccomodationType, 'code');
            $this->msg->info('viAccomodationType.editSuccess', $viAccomodationType['name']);
        }
        else {
            $this->dao->insert('vi_accomodation_type', $viAccomodationType);
            $this->msg->info('viAccomodationType.createSuccess', $viAccomodationType['name']);
        }
        
        $this->ajaxSuccessOrRedirect('/admin/viAccomodationType/all', $viAccomodationType);
    }

    function delete() {
        $code = $this->input->get('code');

        if (! valid::isCode($code)) {
            $this->msg->error('error.invalidId');
            $this->redirect('/admin/viAccomodationType/all');
        }

        // Basic validation
        $viAccomodationType = $this->dao->getByCode('vi_accomodation_type', $code);
        if (! $viAccomodationType) {
            $this->msg->error('error.recordNotFound');
            $this->redirect('/admin/viAccomodationType/all');
        }

        $this->dao->deleteByCode('vi_accomodation_type', $code);

        $this->msg->error('viAccomodationType.deleteSuccess', $viAccomodationType['name']);
        $this->redirect('/admin/viAccomodationType/all');
    }
}
?>
